﻿#include "testQt.h"


testQt::testQt(QWidget* parent): QWidget(parent)
{
    ui.setupUi(this);
    //1.创建老师和学生对象
    this->teacher = new Teacher(this);
    this->stu = new Student(this);
     
    //点击按钮触发下课
   


    //2.0：无参 信号和槽函数连接 =>假设没有重载版本时的调用情况=>只有treat()和hungry()
    /*
        connect(teacher, SIGNAL(hungry()), stu, SLOT(treat())); //QT4写法
        connect(teacher, &Teacher::hungry, stu, &Student::treat);   
    */

    //2.1：无参 信号和槽函数连接 =>有重载版本时的调用情况
    /*
        void(Teacher:: * teacherSignal)() = &Teacher::hungry;
        void(Student:: * studentSlot)() = &Student::treat;
        connect(teacher, teacherSignal, stu, studentSlot);
    
        connect(btn, &QPushButton::clicked, teacher, teacherSignal); //信号可以连接信号
    */


    //2.2：有参 信号和槽函数连接 =>有重载版本时的调用情况
    /*
        void(Teacher:: * teacherSignal)(QString) = &Teacher::hungry;
        void(Student:: * studentSlot)(QString) = &Student::treat;
        connect(teacher, teacherSignal, stu, studentSlot);
        
        //connect(btn, &QPushButton::clicked, teacher, teacherSignal); //err,原因:信号和槽的参数类型不匹配
            //clicked的第一个参数为bool teacherSignal函数的参数为QString,信号和槽函数的参数 必须类型一一对应
    */

    QPushButton* btn = new QPushButton(u8"close Widget", this);
    btn->move(200, 200);
    btn->resize(100, 100);

    connect(btn, &QPushButton::clicked, this, [=]() {
        qDebug() << u8"关闭窗口";
        close(); //this->close(); 关闭窗口
        });

    //3.调用下课函数
    /*ClassOver();*/
}

void testQt::ClassOver()
{
    emit teacher->hungry(u8"宫保鸡丁"); //触发老师饿了信号 并为hungry函数传参
}
